import axios from 'axios'
import Element from 'element-ui'
import router from './router'
import store from './store'
import {paperLists} from "./api/paperMG";


axios.defaults.baseURL = "http://localhost:8082";

// 响应拦截
axios.interceptors.response.use(response => {
    let res = response.data;

    console.log("=================");
    console.log(res);
    console.log("=================");

    if (res.code === 200) {
      return response
    }

    else if(res.code === 412) { //往期问卷状态切换无效
        return response
    }
    else if(res.code === 201) { //手机号返显
        return response
    }
    else if(res.type === 'image/jpeg') { //放行验证码图片
        return response
    }
    else {

      Element.Message.error(response.data.msg, {duration: 3 * 1000});

      return Promise.reject(response.data.msg)
    }
  },
  error => {
      console.log("==============");
    console.log(error);
      console.log("==============");
    if(error.response.data) {
      error.message = error.response.data.msg
    }

    if(error.response.status === 401) {
      store.commit("REMOVE_INFO");
      router.push("/login")
    }
      if(error.response.status === 500) {
        console.log("500");
        store.commit("REMOVE_INFO");
        error.message = error.response.data.msg
      }

    Element.Message.error(error.message, {duration: 3 * 1000})
    return Promise.reject(error)
  }
)